package com.aduerr.v1shouyin.mvp.adpter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.entity.OrderGetPosOrderLEntity;
import com.aduerr.v1shouyin.mvp.activity.OrderDetailActivity;

import java.util.List;

/**
 * @author HI
 * @email dev1024@foxmail.com
 * @time 2018/10/19 17:49
 * @change
 * @chang time
 * @class describe
 */

public class OrderRecordAdapter extends RecyclerView.Adapter<OrderRecordAdapter.ViewHolder> {

    private Context context;
    private List<OrderGetPosOrderLEntity.DataBean.ListBean> lists;

    public OrderRecordAdapter(Context context,
                              List<OrderGetPosOrderLEntity.DataBean.ListBean> lists) {
        this.context = context;
        this.lists = lists;
    }

    public void updateItem(OrderGetPosOrderLEntity.DataBean.ListBean dataBean) {
        int index = lists.indexOf(dataBean);
        if(index >= 0) {
            lists.set(index, dataBean);
            notifyItemChanged(index);
        }
    }

    public void updateData(List<OrderGetPosOrderLEntity.DataBean.ListBean> lists) {
        this.lists = lists;
        notifyDataSetChanged();
    }

    public void addData(List<OrderGetPosOrderLEntity.DataBean.ListBean> lists) {
        for(OrderGetPosOrderLEntity.DataBean.ListBean lb : lists) {
            this.lists.add(lb);
        }
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v =
                LayoutInflater.from(parent.getContext()).inflate(R.layout.item_refund_record, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        OrderGetPosOrderLEntity.DataBean.ListBean dataBean = lists.get(position);
        holder.tvTime.setText((dataBean.getCreatetime()).substring(11, 16));
        holder.tvState.setText(dataBean.getPaystate());
        holder.tvOrderId.setText(dataBean.getOrderid());
        holder.tvMoney.setText("￥" + dataBean.getOrdermoney());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, OrderDetailActivity.class);
                intent.putExtra("datebean", lists.get(position));
                intent.putExtra("repair", "   补打小票");
                intent.putExtra("sourceType", 0);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return lists == null ? 0 : lists.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvTime, tvState, tvOrderId, tvMoney;

        public ViewHolder(View itemView) {
            super(itemView);
            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
            tvState = (TextView) itemView.findViewById(R.id.tv_state);
            tvOrderId = (TextView) itemView.findViewById(R.id.tv_order_id);
            tvMoney = (TextView) itemView.findViewById(R.id.tv_money);
        }
    }
}
